<template>
  <div class="userAgreement">
    <rj-modal
      :title="'用户及隐私协议' + version"
      v-if="visible"
      :visible="true"
      :width="750"
      ok-text="同意"
      cancel-text="不同意"
      :closable="false"
      @cancel="disagreeClick"
      @ok="agreeClick"
    >
      <div
        style="max-height: 500px; overflow-y: auto"
        v-html="agreeMentText"
      ></div>
    </rj-modal>
  </div>
</template>

<script>
import { requestHttp, maccApi } from '@utils/index';
import { RjModal } from '@components';

export default {
  name: 'userAgreement',
  components: {
    'rj-modal': RjModal,
  },
  data() {
    return {
      visible: false,
      version: '',
      agreeMentText: '',
    };
  },
  created() {
    this.getUserState();
  },
  methods: {
    // 获取用户协议状态
    async getUserState() {
      let res = await requestHttp({
        api: maccApi.CLOUD_USERAGREEMENT_STATUS_GET_API,
        method: 'GET',
      });
      console.log(res, '用户协议状态---------');
      if (res && res.code == 0) {
        if (!!!res.data.agree) {
          this.getLeastVersion();
          this.visible = true;
        }
      }
      if (res.code !== 0) {
        this.$message.error(res.msg || '获取用户协议状态失败');
      }
    },
    // 获取最新版本用户协议内容
    async getLeastVersion() {
      let res = await requestHttp({
        api: maccApi.CLOUD_USERAGREEMENT_LASTVERSION_GET_API,
        method: 'GET',
      });
      console.log(res, '协议----------------------------------');
      if (res && res.code == 0) {
        this.version = res.data.version;
        this.agreeMentText = res.data.content;
      }
      if (res.code !== 0) {
        this.$message.error(res.msg);
      }
    },
    async agreeClick() {
      // 同意最新版本用户协议内容
      let res = await requestHttp({
        api: `${maccApi.CLOUD_USERAGREEMENT_LASTVERSION_POST_API.replace(
          '{version}',
          this.version
        ).replace('{agree}', 'true')}`,
        method: 'POST',
      });
      console.log(res, '同意协议----------------------------------');
      if (res && res.code == 0) {
        this.$message.success('签署协议成功');
        this.visible = false;
      }
      if (res.code !== 0) {
        this.$message.error(res.msg || '签署协议失败');
      }
    },
    disagreeClick() {
      this.$store.commit('User/CLEAR_TOKEN');
      this.$store.commit('User/TOP_LOGINOUT');
      location.href = '/webproxy/sso/logout';
      this.visible = false;
    },
  },
};
</script>
